package cn.iocoder.yudao.module.system.controller.admin.book;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.system.controller.admin.book.vo.*;
import cn.iocoder.yudao.module.system.service.book.BookUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Map;


import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 绘本上传")
@RestController
@RequestMapping("/system/book/upload")
@Validated
@Slf4j
public class BookUploadController {

    @Resource
    private BookUploadService bookUploadService;

    @PostMapping("/compressed")
    @Operation(summary = "上传压缩包（临时）")
    public CommonResult<BookUploadRespVO> uploadCompressedFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("sessionUuid") String sessionUuid) throws IOException {
        
        Map<String, String> result = bookUploadService.uploadCompressedFile(file, sessionUuid);
        
        BookUploadRespVO respVO = new BookUploadRespVO();
        respVO.setTempUrl(result.get("minioObjectUrl"));
        respVO.setTempKey(result.get("tempKey"));
        // 不再 setFilePath
        
        return success(respVO);
    }

    @PostMapping("/cover")
    @Operation(summary = "上传封面（临时）")
    public CommonResult<BookUploadRespVO> uploadCoverFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("sessionUuid") String sessionUuid) throws IOException {
        
        Map<String, String> result = bookUploadService.uploadCoverFile(file, sessionUuid);
        
        BookUploadRespVO respVO = new BookUploadRespVO();
        respVO.setTempUrl(result.get("coverUrl"));
        respVO.setTempKey(result.get("tempKey"));
        // 不再 setFilePath
        
        return success(respVO);
    }

    @PostMapping("/submit")
    @Operation(summary = "最终提交处理")
    public CommonResult<Void> submitBookUpload(@RequestBody BookSubmitReqVO reqVO) {
        bookUploadService.submitBookUpload(
                reqVO.getTitle(),
                reqVO.getSessionUuid(),
                reqVO.getCompressedTempKey(),
                reqVO.getCoverTempKey(),
                reqVO.getMinioObjectUrl(),
                reqVO.getCoverUrl(),
                reqVO.getFormatDescription()
        );
        return success(null); // 只返回 code
    }

    @PostMapping("/cleanup")
    @Operation(summary = "清理过期临时文件")
    public CommonResult<Boolean> cleanupExpiredTempFiles() {
        bookUploadService.cleanupExpiredTempFiles();
        return success(true);
    }

    @GetMapping("/validate-session")
    @Operation(summary = "校验会话UUID")
    @Parameter(name = "sessionUuid", description = "会话UUID", required = true)
    public CommonResult<Boolean> validateSessionUuid(@RequestParam("sessionUuid") String sessionUuid) {
        boolean isValid = bookUploadService.validateSessionUuid(sessionUuid);
        return success(isValid);
    }

    @GetMapping("/validate-tempkey")
    @Operation(summary = "校验tempKey")
    @Parameter(name = "sessionUuid", description = "会话UUID", required = true)
    @Parameter(name = "tempKey", description = "tempKey", required = true)
    @Parameter(name = "type", description = "类型（compressed/cover）", required = true)
    public CommonResult<Boolean> validateTempKey(
            @RequestParam("sessionUuid") String sessionUuid,
            @RequestParam("tempKey") String tempKey,
            @RequestParam("type") String type) {
        boolean isValid = bookUploadService.validateTempKey(sessionUuid, tempKey, type);
        return success(isValid);
    }
} 